<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="searchForm" class="mb-20">
      <el-form-item label="关键词">
        <el-input v-model="searchForm.keyword" placeholder="请输入关键词" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 点赞记录表格 -->
    <el-table :data="likeList" border style="width: 100%">
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="opusId" label="作品名称" align="center">
        <template slot-scope="scope">
          {{ OpusNameList[scope.$index] }}
        </template>
      </el-table-column>
      <el-table-column prop="likeUserId" label="用户名称" align="center">
        <template slot-scope="scope">
          {{ UserNameList[scope.$index] }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="点赞时间" align="center">
        <template #default="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="danger" plain @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="mt-20"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      :current-page="currentPage"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getList, deleteLike } from '@/api/oupusLike' // 后期替换为真实接口
import moment from 'moment'

export default {
  name: 'LikeManage',
  data() {
    return {
      likeList: [],
      OpusNameList: [],
      UserNameList: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取点赞列表
    async fetchData() {
      try {
        const res = await getList({
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          ...this.searchForm
        })
        this.likeList = res.opusLikePage.records
        this.total = res.opusLikePage.total
        this.OpusNameList = res.OpusNameList
        this.UserNameList = res.UserNameList
      } catch (error) {
        this.$message.error('获取点赞记录失败')
      }
    },
    // 时间格式化
    formatTime(time) {
      return moment(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 搜索
    handleSearch() {
      this.currentPage = 1
      this.fetchData()
    },
    // 重置搜索
    resetSearch() {
      this.searchForm = {
        opusId: null,
        likeUserId: null
      }
      this.currentPage = 1
      this.fetchData()
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    // 删除点赞记录
    handleDelete(row) {
      this.$confirm('确定删除该点赞记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await deleteLike({ likeId: row.likeId })
          this.$message.success('删除成功')
          this.fetchData()
        } catch (error) {
          this.$message.error('操作失败')
        }
      })
    }
  }
}
</script>

<style scoped>
.mb-20 {
  margin-bottom: 20px;
}

.mt-20 {
  margin-top: 20px;
}
</style>
